package com.renwei.yunlong.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.scwang.smartrefresh.layout.util.DensityUtil;

/**
 * Created by wangqi on 2017/11/29.
 */

public class DialogUtils {

    public interface OnDialogClickListener {
        void onCancel(Dialog dialog);

        void onConfirm(Dialog dialog);
    }

    public interface OnUpdateListener {
        void onUpdate(PopupWindow popupWindow);
    }

    public interface OnGuideListener {
        void onButtonClick(PopupWindow popupWindow, Button button);
    }

    public static void showDialog(Context context, String content, boolean cancelable, final OnDialogClickListener listener) {
        View view = View.inflate(context, R.layout.dialog_notice_layout, null);
        // 实例化 Dialog
        final Dialog updataDialog = new Dialog(context);
        // 获取窗口管理器
        Window dialogWindow = updataDialog.getWindow();
        // 隐藏标题栏
        dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        Button btnCancel = view.findViewById(R.id.btn_cancel);//取消按钮
        Button btnConfirm = view.findViewById(R.id.btn_confirm);//确认按钮
        TextView tvContent = view.findViewById(R.id.tv_content);
        tvContent.setText(Html.fromHtml(content));
        // 设置显示的View
        dialogWindow.setContentView(view);
        updataDialog.setCancelable(cancelable);
        // 设置显示的位置
        dialogWindow.setGravity(Gravity.CENTER);
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        dialogWindow.setBackgroundDrawable(dw);
        updataDialog.show();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCancel(updataDialog);
                }
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onConfirm(updataDialog);
                }
            }
        });
    }
    public static void showSingleDialog(Context context, String content, boolean cancelable, final OnDialogClickListener listener) {
        View view = View.inflate(context, R.layout.dialog_notice_layout, null);
        // 实例化 Dialog
        final Dialog updataDialog = new Dialog(context);
        // 获取窗口管理器
        Window dialogWindow = updataDialog.getWindow();
        // 隐藏标题栏
        dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        Button btnCancel = view.findViewById(R.id.btn_cancel);//取消按钮
        Button btnConfirm = view.findViewById(R.id.btn_confirm);//确认按钮
        TextView tvContent = view.findViewById(R.id.tv_content);
        btnCancel.setVisibility(View.GONE);
        tvContent.setText(Html.fromHtml(content));
        // 设置显示的View
        dialogWindow.setContentView(view);
        updataDialog.setCancelable(cancelable);
        // 设置显示的位置
        dialogWindow.setGravity(Gravity.CENTER);
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        dialogWindow.setBackgroundDrawable(dw);
        updataDialog.show();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCancel(updataDialog);
                }
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onConfirm(updataDialog);
                }
            }
        });
    }

    public static void showUpdatePop(final Context context, View view, String content, String versionNo, String isUpdate, final OnUpdateListener listener) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_update_layout, null);
        final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(false);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setClippingEnabled(false);
        Button btUpdate = contentView.findViewById(R.id.bt_update);//更新按钮
        TextView tvContent = contentView.findViewById(R.id.tv_content);
        TextView tvVersion = contentView.findViewById(R.id.tv_version);
        ImageView iv_close = contentView.findViewById(R.id.iv_close);
        if ("1".equals(isUpdate)) {
            iv_close.setVisibility(View.GONE);
        } else {
            iv_close.setVisibility(View.VISIBLE);
        }
        tvContent.setText(content);
        tvVersion.setText("V" + versionNo);
        btUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onUpdate(popupWindow);
                }
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1.0f);
            }
        });
        backgroundAlpha((Activity) context, 0.5f);
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    public static void showGuidePop(final Context context, View view, int ivRes, String[] message, final OnGuideListener listener, final String... buttons) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_guide_layout, null);
        final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setOutsideTouchable(true);
        ImageView ivLogo = contentView.findViewById(R.id.iv_logo);
        LinearLayout llText = contentView.findViewById(R.id.ll_text);
        LinearLayout llbutton = contentView.findViewById(R.id.ll_button);
        ivLogo.setImageResource(ivRes);
        for (String text : message) {
            TextView tv = new TextView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER_HORIZONTAL;
            params.setMargins(0, 0, 0, DensityUtil.dp2px(3));
            tv.setLayoutParams(params);
            tv.setTextColor(context.getResources().getColor(R.color.white));
            tv.setTextSize(17);
            tv.setText(Html.fromHtml(StringUtils.value(text)));
            llText.addView(tv);
        }
        for (int i = 0; i < buttons.length; i++) {
            Button bt = new Button(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(45));
            params.gravity = Gravity.CENTER_HORIZONTAL;
            params.setMargins(DensityUtil.dp2px(55), 0, DensityUtil.dp2px(55), DensityUtil.dp2px(20));
            bt.setLayoutParams(params);
            bt.setText(buttons[i]);
            bt.setTextColor(context.getResources().getColor(R.color.white));
            bt.setTextSize(DensityUtil.dp2px(6));

            if (i == 0) {
                bt.setBackground(context.getResources().getDrawable(R.drawable.shape_pop_guide_yellow));
            } else if (i == buttons.length - 1) {
                bt.setBackground(context.getResources().getDrawable(R.drawable.shape_pop_guide_trans));
            } else {
                bt.setBackground(context.getResources().getDrawable(R.drawable.shape_pop_guide_red));
            }
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        popupWindow.dismiss();
                        listener.onButtonClick(popupWindow, (Button) view);
                    }
                }
            });
            llbutton.addView(bt);
        }
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    }

    public static void showGuidePop2(final Context context, View view, int ivRes, String[] message, final OnGuideListener listener, final String... buttons) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_guide_layout, null);
        final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setOutsideTouchable(true);
        ImageView ivLogo = contentView.findViewById(R.id.iv_logo);
        LinearLayout llText = contentView.findViewById(R.id.ll_text);
        LinearLayout llbutton = contentView.findViewById(R.id.ll_button);
        ivLogo.setImageResource(ivRes);
        for (String text : message) {
            TextView tv = new TextView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER_HORIZONTAL;
            params.setMargins(0, 0, 0, DensityUtil.dp2px(3));
            tv.setLayoutParams(params);
            tv.setTextColor(context.getResources().getColor(R.color.white));
            tv.setTextSize(17);
            tv.setText(Html.fromHtml(StringUtils.value(text)));
            llText.addView(tv);
        }
        for (int i = 0; i < buttons.length; i++) {
            Button bt = new Button(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(45));
            params.gravity = Gravity.CENTER_HORIZONTAL;
            params.setMargins(DensityUtil.dp2px(55), 0, DensityUtil.dp2px(55), DensityUtil.dp2px(20));
            bt.setLayoutParams(params);
            bt.setText(buttons[i]);
            bt.setTextColor(context.getResources().getColor(R.color.white));
            bt.setTextSize(DensityUtil.dp2px(6));

            if (buttons[i].contains("联泰信息")) {
                bt.setBackground(context.getResources().getDrawable(R.drawable.shape_pop_guide_yellow));
            } else if (buttons[i].contains("邀请对方注册")) {
                bt.setBackground(context.getResources().getDrawable(R.drawable.shape_card_main_color));
            } else {
                bt.setBackground(context.getResources().getDrawable(R.drawable.shape_pop_guide_red));
            }
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        popupWindow.dismiss();
                        listener.onButtonClick(popupWindow, (Button) view);
                    }
                }
            });
            llbutton.addView(bt);
        }
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

}
